//
//  UIButtonView.m
//  jipin
//
//  Created by lcb on 6/4/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "UIButtonView.h"
#import "viewconfig.h"
@interface UIButtonView()
{
    
}
@property(nonatomic,assign) id key;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UIImageView *iconView;
@property(nonatomic,strong) UILabel *nameLable;
@property(nonatomic,strong)void(^actionBlock)(BEventType,UIView *,id,id);
@end

@implementation UIButtonView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _bgView =[[UIView alloc] initWithFrame:frame];
        _bgView.backgroundColor =[UIColor clearColor];
        [self addSubview:_bgView];
        //
        _iconView =[[UIImageView alloc] initWithFrame:CGRectZero];
        [self addSubview:_iconView];
        //
        _nameLable =[[UILabel alloc] initWithFrame:CGRectZero];
        _nameLable.backgroundColor = [UIColor clearColor];
        _nameLable.textColor = UIColorFromRGB(0xcccccc);
        _nameLable.textAlignment = NSTextAlignmentLeft;
        _nameLable.font = FONT(12);
        [self addSubview:_nameLable];
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        [self addGestureRecognizer:singleRecognizer];
        singleRecognizer=nil;
    }
    return self;
}
-(void)dealloc
{
    self.actionBlock=nil;
    self.bgView=nil;
    self.nameLable=nil;
    self.iconView=nil;
//    NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id))callback
{
    self.actionBlock =callback;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.bgView.frame =self.bounds;
   
    self.iconView.frame = CGRectMake(0, (self.bounds.size.height-20)/2, 20, 20);
    self.nameLable.frame = CGRectMake(22,(self.bounds.size.height-20)/2, self.bounds.size.width-22, 20);
   
}
-(void)setImage:(UIImage*)image name:(NSString*)name
{
    self.iconView.image = image;
    self.nameLable.text =name;
    if ([name hasPrefix:@"RMB"]) {
        self.nameLable.font =FONT(12);
        self.nameLable.textColor = UIColorFromRGB(0xFF7725);
    }
    [self layoutSubviews];
}
-(void)handleSingleTapFrom
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_btn,nil,nil,nil);
    }
}
@end
